import type { Effect, Reducer } from 'umi';
import { userSignInfo, userCompany } from '@/services/user';

export interface UserSign {
  name?: string;
  currentMobile: string;
  id: string;
  userId: string;
  certNo?: string;
  tag: number;
  status: number;
  faceFile?: string;
  backFile?: string;
  signUrl?: string;
}

export interface CorpSign {
  code?: string;
  name?: string;
  type?: number;
  corpId?: number;
  signUrl?: string;
  status?: number;
}

export interface SignInfo {
  user: UserSign;
  corp: CorpSign;
}

export interface SignModalState {
  signUser?: Partial<UserSign>;
  signCorp?: Partial<CorpSign>;
  isLoading?: boolean;
  needSign?: boolean;
}

export interface ModelType {
  namespace: string;
  state: SignModalState;
  effects: {
    fetchSign: Effect;
    fetchCompany: Effect;
    fetchUser: Effect;
  };
  reducers: {
    saveSign: Reducer<SignModalState>;
    saveSignUser: Reducer<SignModalState>;
    setCompany: Reducer<SignModalState>;
    changeLoading: Reducer<SignModalState>;
  };
}

const SignModel: ModelType = {
  namespace: 'sign',

  state: {
    signUser: {},
    signCorp: {},
    isLoading: false,
    needSign: false
  },

  effects: {
    *fetchSign(_, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const { success: userS, data: userD } = yield call(userSignInfo);
      const { success: corpS, data: corpD } = yield call(userCompany);
      if (userS && corpS) {
        if (userD && corpD && userD.status == 2 && corpD.status == 2) {
          yield put({
            type: 'saveSign',
            payload: {
              signUser: userD,
              signCorp: corpD,
              isLoading: false,
              needSign: false
            }
          })
        } else {
          yield put({
            type: 'saveSign',
            payload: {
              signUser: userD,
              signCorp: corpD || { name: '', code: '', status: 0},
              isLoading: false,
              needSign: true
            }
          })
        }
      } else {
        yield put({
          type: 'saveSign',
          payload: {
            isLoading: false,
            needSign: true
          }
        })
      }
    },
    *fetchUser(_, { call, put }) {
      const { data } = yield call(userSignInfo);
      yield put({
        type: 'saveSignUser',
        payload: data,
      });
    },
    *fetchCompany(_, { call, put }): any {
      const response = yield call(userCompany);
      yield put({
        type: 'setCompany',
        payload: response,
      });
    }
  },
  reducers: {
    saveSign(state, action) {
      return {
        ...state,
        ...action.payload
      };
    },
    saveSignUser(state, action) {
      return {
        ...state,
        signUser: action.payload || {},
      };
    },
    setCompany(state, action) {
      return {
        ...state,
        province: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        isLoading: action.payload,
      };
    },
  },
};

export default SignModel;
